﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    canvas {
        border-style: solid;
        border-width: 1px;
        border-color: #000
    }
</style>
<script type="text/javascript">
    var addrArray = [];
    function Draw(canvas) {
        canvas.width = canvas.width;
        d2d = canvas.getContext('2d');
        d2d.fillStyle = "rgba(100,150,185,0.5)";
        //d2d.strokeStyle = '#f00';
        d2d.beginPath();
        if (addrArray.length > 0)
            d2d.moveTo(addrArray[0].x, addrArray[0].y)
        for (var i = 1; i < addrArray.length; i++) {
            var addr = addrArray[i];
            d2d.lineTo(addr.x, addr.y);
        }
        d2d.closePath();
        d2d.fill();
        d2d.stroke();
    }

    function revertImage(canvas, x, y) {
        if (canvas.getContext) {
            var context = canvas.getContext('2d');
            // 从指定的矩形区域获取 canvas 像素数组
            var imgdata = context.getImageData(x, y, 3, 3);
            var pixels = imgdata.data;
            console.log(pixels);
            // 遍历每个像素并对 RGB 值进行取反
            // for (var i=0, n=pixels.length; i<n; i+= 4){
            //     pixels[i] = 255-pixels[i];
            //     pixels[i+1] = 255-pixels[i+1];
            //     pixels[i+2] = 255-pixels[i+2];
            //     // pixels[i] = 0;
            //     // pixels[i+1] = 255;
            //     // pixels[i+2] = 255;
            // }
            for (var i = 0; i < pixels.length; i++) {
                pixels[i] = 60;
            }
            // 在指定位置进行像素重绘
            context.putImageData(imgdata, x, y);
            console.log(2);
        }
    }

    window.onload = function () {
        var canvas = document.getElementById('canvas');
        canvas.addEventListener('click', function (evt) {
            addrArray.push({ x: evt.offsetX, y: evt.offsetY });
            console.log(evt.offsetX, evt.offsetY);
            Draw(canvas);
            //revertImage(canvas,evt.clientX,evt.clientY);
            ;
        }, false);

    }
</script>

<body>
    <canvas id="canvas" width="3102" height="3988px" style="position:relative; border: 1px solid black;background-image: url(tonglingmap.png); background-repeat:no-repeat; background-size:100% 100%;">
        Fallback content, in case the browser does not support Canvas.
    </canvas>
</body>

</html>